
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Javascript,JQuery,">
  

  
    <meta name="description" content="深入理解JQuery原理">
  
  
  <link rel="icon" type="image/x-icon" href="/logo.png">
  <title>深入理解JQuery原理 [ 复制粘贴工程师 ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="https://wx.qlogo.cn/mmopen/vi_32/AMpRacDgkTPogKk4gQ5IC3QezP55XoPpicQpnD9Vz3eNz6OPFYyFrFzCa4EGQGH65kPia5YJwQBBvvJpVwQ4lfVg/132">
    <span class="title">复制粘贴工程师</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
  </ul>

</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        深入理解JQuery原理
      </h1>
      <span>
        
        <time class="time" datetime="2019-02-20T03:13:22.000Z">
        2019-02-20
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JQuery/">JQuery</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Javascript/">Javascript</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <p>本文将手把手逐渐实现一个简化版的 <code>JQuery</code>。</p>
<h3 id="先来自己动手简化选择器"><a href="#先来自己动手简化选择器" class="headerlink" title="先来自己动手简化选择器"></a>先来自己动手简化选择器</h3><p><code>base</code> 是一个对象，拥有两个方法，方法名为 <code>$</code>、<code>$$</code>，功能分别为通过 <code>id</code> 和 <code>class</code> 来获取元素。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> base = &#123;</span><br><span class="line">    $:<span class="function"><span class="keyword">function</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">document</span>.getElementById(id);</span><br><span class="line">    &#125;,</span><br><span class="line">    $$:<span class="function"><span class="keyword">function</span>(<span class="params">className</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">document</span>.getElementsByClassName(className);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用</span></span><br><span class="line">base.$(<span class="string">"id"</span>);       <span class="comment">//返回相应的元素对象</span></span><br><span class="line">base.$$(<span class="string">"class"</span>);   <span class="comment">//返回相应的元素对象集合</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h3 id="进阶，向JQuery靠拢"><a href="#进阶，向JQuery靠拢" class="headerlink" title="进阶，向JQuery靠拢"></a>进阶，向JQuery靠拢</h3><p>声明一个全局方法 <code>$</code>，用于返回一个 <code>base</code> 对象。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> $ = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Base();</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Base</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.$ = <span class="function"><span class="keyword">function</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">document</span>.getElementById(id);</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.$$ = <span class="function"><span class="keyword">function</span>(<span class="params">className</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">document</span>.getElementsByClassName(className);</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用</span></span><br><span class="line">$().$(<span class="string">"id"</span>);       <span class="comment">//返回相应的元素对象</span></span><br><span class="line">$().$$(<span class="string">"class"</span>);   <span class="comment">//返回相应的元素对象集合</span></span><br></pre></td></tr></table></figure>
<h3 id="加上css方法"><a href="#加上css方法" class="headerlink" title="加上css方法"></a>加上css方法</h3><p>增加 <code>css</code> 方法，就可以通过 <code>el.css().css()</code> 链式调用的形式给元素增加样式。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> $ = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Base();</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Base</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.elements = []  <span class="comment">//创建一个数组，来保存获取的节点和节点数组</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.$ = <span class="function"><span class="keyword">function</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//因为每一次使用$都会新建对象，所以不需要清空，不会异常</span></span><br><span class="line">        <span class="keyword">this</span>.elements.push(<span class="built_in">document</span>.getElementById(id));</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;  <span class="comment">//一定要返回对象，不然无法进行连缀</span></span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.$$ = <span class="function"><span class="keyword">function</span>(<span class="params">className</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> clss = <span class="built_in">document</span>.getElementsByClassName(className);</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span> ;i &lt; clss.length; i++)&#123;</span><br><span class="line">            <span class="keyword">this</span>.elements.push(clss[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.css = <span class="function"><span class="keyword">function</span>(<span class="params">attr,val</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>;i &lt; <span class="keyword">this</span>.elements.length; i++)&#123;</span><br><span class="line">            <span class="keyword">this</span>.elements[i].style[attr] = val;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用</span></span><br><span class="line">$().$(<span class="string">"id"</span>).css(<span class="string">"color"</span>,<span class="string">"white"</span>).css(<span class="string">"background"</span>,<span class="string">"black"</span>);       <span class="comment">//返回相应的元素对象</span></span><br><span class="line">$().$$(<span class="string">"class"</span>).css(<span class="string">"color"</span>,<span class="string">"red"</span>).css(<span class="string">"border"</span>,<span class="string">"1px dotted black"</span>);   <span class="comment">//返回相应的元素对象集合</span></span><br></pre></td></tr></table></figure>
<h3 id="加上eq方法"><a href="#加上eq方法" class="headerlink" title="加上eq方法"></a>加上eq方法</h3><p>用于遍历元素，添加一个 <code>current</code> 变量即可<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> $ = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Base();</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Base</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.elements = [];  <span class="comment">//创建一个数组，来保存获取的节点和节点数组</span></span><br><span class="line">    <span class="keyword">this</span>.current = <span class="number">-1</span>;   <span class="comment">//用于遍历</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.$ = <span class="function"><span class="keyword">function</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//因为每一次使用$都会新建对象，所以不需要清空，不会异常</span></span><br><span class="line">        <span class="keyword">this</span>.elements.push(<span class="built_in">document</span>.getElementById(id));</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;  <span class="comment">//一定要返回对象，不然无法进行连缀</span></span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.$$ = <span class="function"><span class="keyword">function</span>(<span class="params">className</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> clss = <span class="built_in">document</span>.getElementsByClassName(className);</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span> ;i &lt; clss.length; i++)&#123;</span><br><span class="line">            <span class="keyword">this</span>.elements.push(clss[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.eq = <span class="function"><span class="keyword">function</span>(<span class="params">index</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.current = index;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.css = <span class="function"><span class="keyword">function</span>(<span class="params">attr,val</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.current != <span class="number">-1</span>)<span class="keyword">this</span>.elements[<span class="keyword">this</span>.current].style[attr] = val;</span><br><span class="line">        <span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>;i &lt; <span class="keyword">this</span>.elements.length; i++)&#123;</span><br><span class="line">              <span class="keyword">this</span>.elements[i].style[attr] = val;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用</span></span><br><span class="line">$().$(<span class="string">"id"</span>).css(<span class="string">"color"</span>,<span class="string">"white"</span>).css(<span class="string">"background"</span>,<span class="string">"black"</span>);       <span class="comment">//返回相应的元素对象</span></span><br><span class="line">$().$$(<span class="string">"class"</span>).eq(<span class="number">1</span>).css(<span class="string">"color"</span>,<span class="string">"red"</span>).css(<span class="string">"border"</span>,<span class="string">"1px dotted black"</span>);   <span class="comment">//返回相应的元素对象集合</span></span><br></pre></td></tr></table></figure></p>
<h3 id="通过-“str”-来获取元素"><a href="#通过-“str”-来获取元素" class="headerlink" title="通过$(“str”)来获取元素"></a>通过$(“str”)来获取元素</h3><p>进一步封装 <code>$</code> 函数，从而使得调用方法对标 <code>JQuery</code>。</p>
<p><code>$(&#39;#id&#39;).css().css()</code></p>
<p><code>$(&#39;.class&#39;).eq(2).css().css()</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> $ = <span class="function"><span class="keyword">function</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> base = <span class="keyword">new</span> Base();</span><br><span class="line">    <span class="keyword">if</span>(str[<span class="number">0</span>] == <span class="string">"#"</span>)&#123;</span><br><span class="line">      base.$(str.substr(<span class="number">1</span>,str.length - <span class="number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span>(str[<span class="number">0</span>] == <span class="string">'.'</span>)&#123;</span><br><span class="line">      base.$$(str.substr(<span class="number">1</span>,str.length - <span class="number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span>&#123;</span><br><span class="line">      <span class="comment">//TagName</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> base;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Base</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.elements = [];  <span class="comment">//创建一个数组，来保存获取的节点和节点数组</span></span><br><span class="line">    <span class="keyword">this</span>.current = <span class="number">-1</span>;   <span class="comment">//用于遍历</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.$ = <span class="function"><span class="keyword">function</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//因为每一次使用$都会新建对象，所以不需要清空，不会异常</span></span><br><span class="line">        <span class="keyword">this</span>.elements.push(<span class="built_in">document</span>.getElementById(id));</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;  <span class="comment">//一定要返回对象，不然无法进行连缀</span></span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.$$ = <span class="function"><span class="keyword">function</span>(<span class="params">className</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> clss = <span class="built_in">document</span>.getElementsByClassName(className);</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span> ;i &lt; clss.length; i++)&#123;</span><br><span class="line">            <span class="keyword">this</span>.elements.push(clss[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.eq = <span class="function"><span class="keyword">function</span>(<span class="params">index</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.current = index;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.css = <span class="function"><span class="keyword">function</span>(<span class="params">attr,val</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.current != <span class="number">-1</span>)<span class="keyword">this</span>.elements[<span class="keyword">this</span>.current].style[attr] = val;</span><br><span class="line">        <span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>;i &lt; <span class="keyword">this</span>.elements.length; i++)&#123;</span><br><span class="line">              <span class="keyword">this</span>.elements[i].style[attr] = val;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用</span></span><br><span class="line">$(<span class="string">"#id"</span>).css(<span class="string">"color"</span>,<span class="string">"white"</span>).css(<span class="string">"background"</span>,<span class="string">"black"</span>);       <span class="comment">//返回相应的元素对象</span></span><br><span class="line">$(<span class="string">".class"</span>).eq(<span class="number">1</span>).css(<span class="string">"color"</span>,<span class="string">"red"</span>).css(<span class="string">"border"</span>,<span class="string">"1px dotted black"</span>);   <span class="comment">//返回相应的元素对象集合</span></span><br></pre></td></tr></table></figure>
    </div>

    <div>全文完。</div>
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#先来自己动手简化选择器"><span class="toc-text">先来自己动手简化选择器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#进阶，向JQuery靠拢"><span class="toc-text">进阶，向JQuery靠拢</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#加上css方法"><span class="toc-text">加上css方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#加上eq方法"><span class="toc-text">加上eq方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#通过-“str”-来获取元素"><span class="toc-text">通过$(“str”)来获取元素</span></a></li></ol>
  </div>


  </div>
</div>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share" style="width: 100%;">
  <!-- <img src="https://kevinofneu-blog-static.oss-cn-beijing.aliyuncs.com/static/2018-12-10-qrcode_for_gh_ffacf5722095_258.jpg" alt="Running Geek" style="margin: auto; display: block;"/>

  <div style="margin: auto; text-align: center; font-size: 0.8em; color: grey;">老铁们关注走一走，不迷路</div> -->

</div>

  
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2019/02/20/Javascript内存结构分析/" rel="next" title="Javascript内存结构分析">
          Javascript内存结构分析
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2019/02/28/Github小白指南/" rel="prev" title="Github小白指南">
            Github小白指南
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


    </div>

    
    <div id="vcomments" style="max-width: 800px; margin: 16px auto;"></div>
  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="/">首页</a> |
        <a class="bottom-item" href="https://github.com/TJ-XiaJiaHao" target="_blank">GitHub</a> |
        <a class="bottom-item">联系邮箱：1452806@tongji.edu.cn</a>
    </div>
</footer>
  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<script type="text/javascript">
  new Valine({
    el: '#vcomments',
    appId: '8CwYqXS8IoFbvaA9PmgqGjTf-gzGzoHsz',
    appKey: 'z5bVoSPDPhd8hzlXvDey5Hpz',
    verify:false,
    placeholder: '评论留言'
  })
</script>
</html>
